package swd.work.conf_form_fields_detail;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
    QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        String lstrUserNO = request.getSession().getAttribute("u_id").toString();
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        DataView DataView = null;
        DataViewForm DataViewForm = null;

        DataView = new DataView(gqbnDAO);
        DataViewForm = new DataViewForm();

        request.setAttribute("DataViewForm", DataViewForm);

        Business business = new Business(gqbnDAO);

        try {
            if(view.getPrgAction()!=null && view.getPrgAction().equals("save")){
                business.batchUpdateData(view);
            }

            business.setBtnShowMode(view,view.getPrgAction(),view.getPrgStatus());
            view.setDataViewHtml(business.getDataViewHtml(DataView,DataViewForm,view,lstrUserNO));


            return "";
        } catch (Exception e) {
            view.setOnloadScript("alert('Program error please contact IT.');");
            e.printStackTrace();
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
